package cn.rongdao.jrkabao.ui.cashcoupon;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.cashcoupon.SelectCashCouponBean;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.model.cashcoupon.SelectCashCouponModel;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.CashEmptyUIComponent;
import cn.rongdao.jrkabao.ui.cashcoupon.adapter.SelectCashCouponAdapter;
import cn.rongdao.jrkabao.ui.cashcoupon.adapter.SelectCashCouponViewHolder;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;

/**
 * Created by jack on 2017/8/12
 */

public class SelectCashCouponActivity extends BaseAutoFinishActivity implements FRBaseRecyclerAdapter.OnItemClickListener<SelectCashCouponViewHolder> {

    public static final String AMOUNT = "amount";

    public static final String CASH_COUPON_POSITION = "cash_coupon_position";

    public static final String CASH_COUPON_MONEY = "cash_coupon_money";

    public static final String CASH_COUPON_ID = "cash_coupon_id";

    public static final String PASS_WAY_ID="pass_way_id";

    public static final int REQUEST_CODE_SELECT_CASH_COUPON = 1110;
    private String passWayId;

    public static void start(Activity activity, String amount, int position,String passWayId) {
        Bundle bundle = new Bundle();
        bundle.putString(AMOUNT, amount);
        bundle.putString(PASS_WAY_ID, passWayId);
        bundle.putInt(CASH_COUPON_POSITION, position);

        FRStartActivity.startForResult(activity, SelectCashCouponActivity.class, bundle, REQUEST_CODE_SELECT_CASH_COUPON);
    }

    @BindView(R.id.ascc_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.ascc_empty_view)
    CashEmptyUIComponent cashEmptyUIComponent;
    @BindView(R.id.ascc_ptrframelayout)
    PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;
    @BindView(R.id.ascc_tv_cash_coupon_num)
    TextView tvCashCouponNum;
    @BindView(R.id.ascc_tv_no_use_cash_coupon)
    TextView tvNoUseCashCoupon;

    private String amount;

    private SelectCashCouponAdapter mAdapter;

    private int cashCouponPosition;


    @Override
    protected void initView() {
        initTitleBar();
        amount = getIntent().getStringExtra(AMOUNT);
        passWayId = getIntent().getStringExtra(PASS_WAY_ID);
        cashCouponPosition = getIntent().getIntExtra(CASH_COUPON_POSITION, -1);
        if (cashCouponPosition == -1) {
            tvNoUseCashCoupon.setSelected(true);
        } else {
            tvNoUseCashCoupon.setSelected(false);
        }
        mAdapter = new SelectCashCouponAdapter(thisActivity(), this);
        ptrRecyclerViewUIComponent.setBackground(R.color.layout_bg);
        ptrRecyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(this));
        ptrRecyclerViewUIComponent.setAdapter(mAdapter);

        initListener();
    }

    private void initListener() {
        ptrRecyclerViewUIComponent.delayRefresh();
        ptrRecyclerViewUIComponent.setOnPullToRefreshListener(new OnPullToRefreshListener() {
            @Override
            public void onPullToRefresh() {
                getData();
            }
        });

        tvNoUseCashCoupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResultFinish(-1, "", "");
            }
        });
    }

    private void setResultFinish(int position, String amount, String cashCouponId) {
        Intent intent = new Intent();
        intent.putExtra(CASH_COUPON_POSITION, position);
        intent.putExtra(CASH_COUPON_MONEY, amount);
        intent.putExtra(CASH_COUPON_ID, cashCouponId);
        setResult(RESULT_OK, intent);
        finish();
    }

    private void getData() {
        SelectCashCouponModel.getInstance().execute(passWayId,amount, new FRBaseObserver<SelectCashCouponBean>(this) {
            @Override
            protected void onBaseNext(SelectCashCouponBean data) {
                if (data.getNum() > 0 || (null != data.getList() && data.getList().size() > 0)) {
                    tvCashCouponNum.setText(FRCollection.appendAsString("有", FRString.valueOf(data.getNum()), "张券可用", ""));
                    ptrRecyclerViewUIComponent.setVisibility(View.VISIBLE);
                    if (cashCouponPosition >= 0) {
                        SelectCashCouponBean.ListBean listBean = data.getList().get(cashCouponPosition);
                        listBean.setSelect(true);
                        data.getList().set(cashCouponPosition, listBean);
                    }
                    mAdapter.setDataList(data.getList());
                } else {
                    tvCashCouponNum.setText("没有现金券可用");
                    cashEmptyUIComponent.initCashCouponEmpty(1);
                    ptrRecyclerViewUIComponent.setVisibility(View.GONE);
                }
                runUI();
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                runUI();
                cashEmptyUIComponent.initCashCouponEmpty(1);
                ptrRecyclerViewUIComponent.setVisibility(View.GONE);
            }
        });
    }

    private void runUI() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ptrRecyclerViewUIComponent.refreshComplete();
            }
        });
    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_select_cash_coupon;
    }

    private void initTitleBar() {
        titleBarUIComponent.initTitle("选择现金券");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        titleBarUIComponent.initRightTextWithColor("使用说明", R.color.white, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.start(thisActivity(), InfoConstant.CASH_COUPON_USE_EXPLIAN);
            }
        });
    }

    @Override
    public void onItemClick(SelectCashCouponViewHolder selectCashCouponViewHolder, int position) {
        SelectCashCouponBean.ListBean listBean = mAdapter.getDataList().get(position);
        if (listBean.getIs_valid() == 1 && !listBean.isSelect()) {
            mAdapter.setAllUnSelected();
            listBean.setSelect(true);
            mAdapter.updateItem(position, listBean);
            setResultFinish(position, listBean.getAmount(), listBean.getId());
        }
    }
}